package controllers

import (
	"encoding/json"
	"errors"
	//"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	// "lottery/constant"
	"lottery/models"
	// "lottery/utils"
	"lottery/services/gamedata"
	"strconv"
)

type PolicyCtl struct {
	BaseCtl
}

func (p *PolicyCtl) UpdatePolicy() {
	if !p.isLogin() {
		p.SetJson("请先登录！", nil)
		return
	}
	policy := &models.PolicyData{}
	err := json.Unmarshal(p.Ctx.Input.RequestBody, policy)
	if err != nil {
		p.SetJson(nil, err)
		return
	}
	userId := p.GetUserId()
	policy.UserId = userId
	//beego.Error("UpdatePolicy", policy)
	if policy.MinRate <= -100 || policy.MaxRate >= 100 || policy.MinRate > policy.MaxRate {
		p.SetJson(nil, errors.New("水位设置不正确"))
		return
	}
	if models.ExistsUserId(userId) {
		//用户ID不存在，说明还没设置过
		err = models.UpdatePolicData(policy)
		//beego.Error("更新策略", err)
		if err != nil {
			p.SetJson(nil, err)
			return
		}
		err = models.UpdatePolicy(policy.Price, userId)
		if err != nil {
			p.SetJson(nil, err)
			return
		}
		p.SetJson("更新策略成功！", nil)
		return
	}

	err = models.SavePolicyData(policy)
	if err != nil {
		p.SetJson(nil, err)
		return
	}
	p.SetJson("新增策略成功", nil)
	return
}
func (p *PolicyCtl) DelPolicy() {
	if !p.isLogin() {
		p.SetJson("请先登录！", nil)
		return
	}
	Id := p.Input().Get("policyId")

	policyId, err := strconv.ParseInt(Id, 10, 64)
	if err != nil {
		p.SetJson(nil, err)
		return
	}
	err = models.DeletePolicy(policyId)
	if err != nil {
		p.SetJson(nil, err)
		return
	}
	p.SetJson("删除成功!", nil)
	return
}
func (p *PolicyCtl) BindMatch() {
	if !p.isLogin() {
		p.SetJson("请先登录！", nil)
		return
	}
	policy := &models.Policy{}
	err := json.Unmarshal(p.Ctx.Input.RequestBody, policy)

	if err != nil {
		p.SetJson(nil, err)
		return
	}
	if policy.Platform1Id <= 0 || policy.Platform1GameId <= 0 {
		p.SetJson(nil, errors.New("绑定赛事信息不全"))
	}
	if policy.Platform2Id <= 0 || policy.Platform2GameId <= 0 {
		p.SetJson(nil, errors.New("绑定赛事信息不全"))
	}
	userId := p.GetUserId()

	if !models.ExistsPlatformIdAndPlatformGameId(policy.Platform1Id, policy.Platform1GameId) {
		p.SetJson(nil, errors.New("赛事不存在"))
		return
	}
	if !models.ExistsPlatformIdAndPlatformGameId(policy.Platform2Id, policy.Platform2GameId) {
		p.SetJson(nil, errors.New("赛事不存在"))
		return
	}

	if models.ExistsPolicy(policy, userId) {
		p.SetJson(nil, errors.New("已经绑定过了!"))
		return
	}

	err = models.SavePolicy(policy, userId)
	if err != nil {
		p.SetJson(nil, err)
		return
	}
	p.SetJson("绑定成功！", nil)
}

func (p *PolicyCtl) GetAttenList_temp() { //获取用户关注的队列

	if !p.isLogin() {
		p.SetJson("请先登录！", nil)
		return
	}

	userId := p.GetUserId()
	page_tmp := p.GetString("page", "0")
	page, err := strconv.Atoi(page_tmp)
	policys, pageCount, err := models.GetPolicyList(userId, page, 0)
	if err != nil {
		p.SetJson(nil, err)
		return
	}
	var gameDatalist map[string][]*models.GameData
	gameDatalist = make(map[string][]*models.GameData)
	var policylist []*models.PolicyAttenData
	policylist = make([]*models.PolicyAttenData, 0, 50)
	data_temp := &models.PolicyAttenData{}

	for _, policy := range policys {
		data_temp = new(models.PolicyAttenData)
		data_temp.Id = policy.Id
		data_temp.UserId = policy.UserId
		data_temp.MinRate = policy.MinRate
		data_temp.MaxRate = policy.MaxRate
		data_temp.Price = policy.Price
		data_temp.Platform1Id = policy.Platform1Id
		data_temp.Platform1AgentId = policy.Platform1AgentId
		data_temp.Platform1GameId = policy.Platform1GameId
		data_temp.Platform2Id = policy.Platform2Id
		data_temp.Platform2AgentId = policy.Platform2AgentId
		data_temp.Platform2GameId = policy.Platform2GameId
		data_temp.CreateTime = policy.CreateTime
		data_temp.PageCount = pageCount
		//获取 第一个网站的账号
		data_temp.Agent1, err = models.GetAgentOne(data_temp.Platform1AgentId)
		if err != nil {
			logs.Debug("获取第一个网站的账号出错,错误信息==》", err)
			data_temp.Agent1 = new(models.Agent)
		}
		data_temp.Agent2, err = models.GetAgentOne(data_temp.Platform2AgentId)
		if err != nil {
			logs.Debug("获取第二个网站的账号出错,错误信息==》", err)
			data_temp.Agent2 = new(models.Agent)
		}

		data_temp.Game1, err = models.GetGameInfo(data_temp.Platform1Id, data_temp.Platform1GameId) //获取球赛相关信息

		if err != nil {
			logs.Debug("获取第一个网站的赛事信息出错,错误信息==》", err)
			data_temp.Game1 = new(models.Game)
		}

		data_temp.Game2, err = models.GetGameInfo(data_temp.Platform2Id, data_temp.Platform2GameId) //获取球赛相关信息

		if err != nil {
			logs.Debug("获取第二个网站的赛事信息出错,错误信息==》", err)
			data_temp.Game2 = new(models.Game)
		}

		//下面开始获取  水位信息
		key := strconv.Itoa(data_temp.Platform1Id) //把平台ID变成字符串
		isset := false
		if gameDatas_temp, ok := gameDatalist[key]; ok {
			//如果平台的key存在  那么代表获取过了，就不需要重复获取了
			for _, gameData := range gameDatas_temp {
				if gameData.GameId == data_temp.Game1.Id {
					isset = true
					data_temp.GameData1 = gameData
					break
				}
			}
			if !isset {
				data_temp.GameData1 = new(models.GameData)
			}

		} else {
			gameDatas, ok := gamedata.GetNewGameData(data_temp.Platform1Id)
			//logs.Debug("gameDatas1", data_temp.Platform1Id, gameDatas)
			if len(gameDatas) > 0 {
				if ok {
					//先往map里面赋值先
					gameDatalist[key] = gameDatas
					for _, gameData := range gameDatas {
						if gameData.GameId == data_temp.Game1.Id {
							isset = true
							data_temp.GameData1 = gameData
							break
						}
					}
				}
				if !isset {
					data_temp.GameData1 = new(models.GameData)
				}
			} else {
				if !isset {
					data_temp.GameData1 = new(models.GameData)
				}
			}

		}

		//第二个网站
		key = strconv.Itoa(data_temp.Platform2Id) //把平台ID变成字符串
		isset = false
		if gameDatas_temp, ok := gameDatalist[key]; ok {
			//如果平台的key存在  那么代表获取过了，就不需要重复获取了

			for _, gameData := range gameDatas_temp {
				if gameData.GameId == data_temp.Game2.Id {
					data_temp.GameData2 = gameData
					isset = true
					break
				}
			}

			if !isset {
				data_temp.GameData2 = new(models.GameData)
			}

		} else {
			gameDatas, ok := gamedata.GetNewGameData(data_temp.Platform2Id)
			//logs.Debug("gameDatas2", data_temp.Platform2Id, gameDatas)
			if ok {
				//先往map里面赋值先
				if len(gameDatas) > 0 {
					gameDatalist[key] = gameDatas
					for _, gameData := range gameDatas {
						if gameData.GameId == data_temp.Game2.Id {
							data_temp.GameData2 = gameData
							isset = true
							break
						}
					}
				}

				if !isset {
					data_temp.GameData2 = new(models.GameData)
				}
			} else {
				if !isset {
					data_temp.GameData2 = new(models.GameData)
				}
			}

		}

		policylist = append(policylist, data_temp)
	}

	p.SetJson(policylist, nil)
	return
}
func (p *PolicyCtl) GetAllGameData() {
	if !p.isLogin() {
		p.SetJson("请先登录！", nil)
		return
	}
	var gameDatalist map[string][]*models.GameData
	gameDatalist = make(map[string][]*models.GameData)

	game_temp, ok := gamedata.GetNewGameData(1)
	if !ok {
		p.SetJson(nil, errors.New("获取数据失败！"))
		return
	}
	key := strconv.Itoa(1)
	gameDatalist[key] = game_temp

	game_temp, ok = gamedata.GetNewGameData(3)
	if !ok {
		p.SetJson(nil, errors.New("获取数据失败！"))
		return
	}
	key = strconv.Itoa(3)
	gameDatalist[key] = game_temp

	p.SetJson(gameDatalist, nil)
	return
}

func (p *PolicyCtl) GetAttenList() {
	if !p.isLogin() {
		p.SetJson("请先登录！", nil)
		return
	}
	userId := p.GetUserId()
	page_tmp := p.GetString("page", "0")
	page, err := strconv.Atoi(page_tmp)
	policys, pageCount, err := models.GetPolicyList(userId, page, 0)
	if err != nil {
		p.SetJson(nil, err)
		return
	}

	//先获取出用户的全部账号
	agents, err := models.GetUserAgents(userId)
	if err != nil {
		p.SetJson(nil, err)
		return
	}

	gameIdSilce := make([]int64, 0, 50)

	for _, policy := range policys {
		gameIdSilce = append(gameIdSilce, policy.Platform1GameId)
		gameIdSilce = append(gameIdSilce, policy.Platform2GameId)
	}

	games, err := models.GetSilceGameInfo(gameIdSilce)
	if err != nil {
		logs.Debug("GetSilceGameInfo ctroller", err)
		p.SetJson(nil, err)
	}

	//获取临时数组
	var policy_att_list []*models.PolicyAttenData
	policy_att_list = make([]*models.PolicyAttenData, 0, 50)
	data_temp := &models.PolicyAttenData{}

	for _, policy := range policys {
		data_temp = new(models.PolicyAttenData)
		data_temp.Id = policy.Id
		data_temp.UserId = policy.UserId
		data_temp.MinRate = policy.MinRate
		data_temp.MaxRate = policy.MaxRate
		data_temp.Price = policy.Price
		data_temp.Platform1Id = policy.Platform1Id
		data_temp.Platform1AgentId = policy.Platform1AgentId
		data_temp.Platform1GameId = policy.Platform1GameId
		data_temp.Platform2Id = policy.Platform2Id
		data_temp.Platform2AgentId = policy.Platform2AgentId
		data_temp.Platform2GameId = policy.Platform2GameId
		data_temp.CreateTime = policy.CreateTime
		data_temp.PageCount = pageCount

		isset1 := false
		isset2 := false
		//获取game
		for _, game := range games {
			if policy.Platform1GameId == game.Id {
				data_temp.Game1 = game
				isset1 = true
			}
			if policy.Platform2GameId == game.Id {
				data_temp.Game2 = game
				isset2 = true
			}
			if isset1 && isset2 {
				break
			}
		}

		if !isset1 {
			data_temp.Game1 = new(models.Game)
		}
		if !isset2 {
			data_temp.Game2 = new(models.Game)
		}
		isset1 = false
		isset2 = false

		for _, agent := range agents {
			if policy.Platform1AgentId == agent.Id {
				data_temp.Agent1 = agent
				isset1 = true
			}
			if policy.Platform2AgentId == agent.Id {
				data_temp.Agent2 = agent
				isset2 = true
			}
			if isset1 && isset2 {
				break
			}
		}

		if !isset1 {
			data_temp.Agent1 = new(models.Agent)
		}
		if !isset2 {
			data_temp.Agent2 = new(models.Agent)
		}
		data_temp.GameData1 = new(models.GameData)
		data_temp.GameData2 = new(models.GameData)
		policy_att_list = append(policy_att_list, data_temp)
	}
	p.SetJson(policy_att_list, nil)
	return
}

func (p *PolicyCtl) GetUserPolicy() { //获取用户的策略数据
	if !p.isLogin() {
		p.SetJson("请先登录！", nil)
		return
	}

	userId := p.GetUserId()
	policydata, err := models.GetPolicyData(userId)
	if err != nil {
		p.SetJson(nil, err)
		return
	}

	p.SetJson(policydata, nil)
	return
}
